package com.yunquan.ohana.ui.calendar.lists.list

import androidx.core.graphics.toColorInt
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.binding.command.BindingConsumer
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.ListsItemEntity
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.utils.ToastUtils
import com.yunquan.ohana.utils.Utils
import io.reactivex.rxjava3.disposables.Disposable
import okhttp3.RequestBody.Companion.toRequestBody

class ListsListItemViewModel(
    viewModel: ListsListViewModel,
    val data: ListsItemEntity,
) : ItemViewModel<ListsListViewModel>(viewModel) {
    val content = ObservableField(data.content)
    val inner = ObservableField<Int>()
    val normal = ObservableField<Int>()
    val textColor = ObservableField<Int>()
    val complete = ObservableField(data.type == 2)
    val isSelected = ObservableField<Boolean>()
    val checked = ObservableField(false)
    val checkIcon = ObservableField(R.mipmap.ic_sync_device_normal)

    init {
        if (data.type == 2) {
            textColor.set(Utils.instance.context.getColor(R.color.c_ff8b8b8b))
            inner.set(Utils.instance.context.getColor(R.color.c_ff8b8b8b))
            normal.set(Utils.instance.context.getColor(R.color.c_F6))
        } else {
            textColor.set(Utils.instance.context.getColor(R.color.c_20))
            inner.set(data.getColorData().inner.toColorInt())
            normal.set(data.getColorData().outer.toColorInt())
        }
    }

    fun setSelected(b: Boolean) {
        isSelected.set(b)
    }

    fun setChecked(b: Boolean) {
        checked.set(b)
        if (b) {
            checkIcon.set(R.mipmap.ic_sync_device_check)
        } else {
            checkIcon.set(R.mipmap.ic_sync_device_normal)
        }
        viewModel.changeSelected()
    }

    val onCheckedChange = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val b = checked.get()!!
            setChecked(!b)
        }
    })

    val onCompleteChange = BindingCommand(object : BindingConsumer<Boolean> {
        override fun call(t: Boolean) {
            data.type = if (t) 2 else 1
            changeStatus()
        }
    })

    private fun changeStatus() {
        val map = mutableMapOf<String, Any?>()
        map["deviceId"] = viewModel.mDeviceId
        map["updateListsContentDtoList"] = arrayListOf(data)
        val body = Gson().toJson(map).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.updateListsContentType(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<Boolean>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    viewModel.showLoading()
                }

                override fun onSuccess(result: Boolean?) {
                    super.onSuccess(result)
                    if (result == true) {
                        viewModel.getListsItem()
                    } else {
                        ToastUtils.showShort(R.string.failed_to_modify_state)
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    viewModel.hideLoading()
                }
            })
    }
}